package com.pms.servlet;


import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.pms.dao.MessagesDao;
import com.pms.entity.Messages;
import com.pms.util.DateUtil;
import com.pms.util.UU;

public class MessagesServlet extends HttpServlet {
	
	MessagesDao dao=new MessagesDao();

	private static final long serialVersionUID = 1L;
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		   String m=request.getParameter("_m");
		   if("".equals(m)||m==null){
			   findmessages(request,response);
			   
		   }else if("save".equals(m)){
			   save(request,response);
			   
		   }
	}
	private void save(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
		String content=request.getParameter("content");
		String id=UU.getPk();
		String t_user_id=request.getParameter("t_user_id");
		String t_project_id=request.getParameter("t_project_id");
		String createtime=DateUtil.getNow();
		System.out.println(content);
		System.out.println(t_user_id);
		System.out.println(t_project_id);
		
			dao.save(id, content, createtime, t_user_id, t_project_id);
		response.sendRedirect("messages.jspx");
		
	}
	private void findmessages(HttpServletRequest request,
			HttpServletResponse response)throws ServletException, IOException {
		
		    MessagesDao mess=new MessagesDao();
		    List<Messages> messages=mess.findAll();
		    request.setAttribute("messages", messages);
		    request.getRequestDispatcher("/WEB-INF/views/main.jsp").forward(request, response);
		    
		

	}
}
